﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
//using System.ComponentModel.Collections;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace NCS_CS
{
  partial class MainForm : Form {

    const string _filename = "gamedata.dat";

    public MainForm() { InitializeComponent(); }

    private void MainForm_Load(object sender, EventArgs e) {
      // Custom add sound column because the designer forgets (Beta problem)
      DataGridViewPronunciationColumn clm = new DataGridViewPronunciationColumn();
      clm.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
      clm.DataPropertyName = "Pronunciation";
      clm.HeaderText = "Pronunciation";
      clm.Name = "Pronunciation";
      clm.ValueType = typeof(byte[]);
      clm.Width = 40;

      DataGridViewCellStyle style = new DataGridViewCellStyle();
      style.Alignment = DataGridViewContentAlignment.MiddleCenter;
      clm.DefaultCellStyle = style;

      this.wordDataGridView.Columns.Add(clm);

      // Open game data file
      SortableBindingList<Word> source = new SortableBindingList<Word>();
      //source.Load(_filename);
      this.wordBindingSource.DataSource = source;
    }

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
      SortableBindingList<Word> list = this.wordBindingSource.List as SortableBindingList<Word>;
      if( list != null ) {
        //list.Save(_filename);
      }
    }

    private void newToolStripMenuItem_Click(object sender, EventArgs e) {
      this.wordBindingSource.DataSource = new SortableBindingList<Word>();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
      this.Close();
    }
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
      MessageBox.Show("Chinese Pronunciation Game Data Application, by Michael Weinhardt", Application.ProductName);
    }

    private void wordDataGridView_DragOver(object sender, DragEventArgs e) {
      Point point = this.wordDataGridView.PointToClient(new Point(e.X, e.Y));
      DataGridView.HitTestInfo hti = this.wordDataGridView.HitTest(point.X, point.Y);

      e.Effect = DragDropEffects.None;

      if( hti.RowIndex == -1 ) return;

      string file = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
      if( file == null ) return;

      // TODO: Maybe see how this can be a switch statement instead

      // Image column?
      if( (this.wordDataGridView.Columns[hti.ColumnIndex].Name == "Character") ) {
        if( Path.GetExtension(file).ToUpper() == ".BMP" ) {
          e.Effect = DragDropEffects.Copy;
          return;
        }
      }

      // Sound column?
      if( (this.wordDataGridView.Columns[hti.ColumnIndex].Name == "Pronunciation") ) {
        if( Path.GetExtension(file).ToUpper() == ".WAV" ) {
          e.Effect = DragDropEffects.Copy;
          return;
        }
      }
    }
    private void wordDataGridView_DragDrop(object sender, DragEventArgs e) {
      Point point = this.wordDataGridView.PointToClient(new Point(e.X, e.Y));
      DataGridView.HitTestInfo hti = this.wordDataGridView.HitTest(point.X, point.Y);

      if( hti.RowIndex == -1 ) return;

      string file = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
      if( file == null ) return;

      if( (this.wordDataGridView.Columns[hti.ColumnIndex].Name == "Character") ) {
        this.wordDataGridView.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Value = Bitmap.FromFile(file);
        return;
      }

      if( (this.wordDataGridView.Columns[hti.ColumnIndex].Name == "Pronunciation") ) {
        FileStream stream = File.Open(file, FileMode.Open);
        byte[] sound = new byte[stream.Length];
        stream.Read(sound, 0, (int)stream.Length - 1);
        this.wordDataGridView.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Value = sound;
      }
    }
  }
}